package com.atzongmaotan.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Random;

/**
 * ClassName:    HyperLogLogService
 * Package:  com.atzongmaotan.service
 * Description:
 *
 * @Author: interface
 * @Create: 2024/9/30 - 下午3:37
 * @Version: v1.0
 */
@Service
@Slf4j
public class HyperLogLogService {
    @Autowired
    private RedisTemplate redisTemplate;
    @PostConstruct
    public void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String ip = "";
                for (int i = 0; i < 200; i++) {
                    Random random = new Random();
                    ip = random.nextInt(256) + "."
                    + random.nextInt(256) + "."
                    + random.nextInt(256)
                    + random.nextInt(256);
                    redisTemplate.opsForHyperLogLog().add("hyperloglog", ip);
                    log.info("统计首次访问ip的次数 ip:{},key:{}",ip,"hyperloglog");
                }
            }
        }).start();
    }
    public void count(){
        Long hyperloglog = redisTemplate.opsForHyperLogLog().size("hyperloglog");
        log.info("统计次数位:{}",hyperloglog);
    }
}
